package translateUi
{
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.OutputProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import utils.StringTool;
	
	/**
	 * 语言转换，中文转 其它 繁体、韩文
	 */
	public class LangReplaceUiMan
	{
		private static var _ins:LangReplaceUiMan;
		public static function get ins():LangReplaceUiMan
		{
			if(!_ins){
				_ins = new LangReplaceUiMan();
			}
			return _ins;
		}
		
		public var mTimer:Timer;
		private var urlArr:Array = [];
		private var curTime:int;
		
		private const LANG_SPLIT:String = "==";
		private const LANG_FILE_CEHUA:String = "lang_ui_cehua.txt";
		private var _langDic:Object = {};
		private var _langLoaded:Boolean = false;
		private var _urlLoader:URLLoader;
		
		private var _timer:Timer;
		private var _cfgStream:FileStream;
		
		/**
		 * 转换类型：1简体转繁体，2繁体转简体，3简体转韩文
		 */
		public var replaceType:int = 1;
		
		/**
		 * 已转换好的语言包
		 */
		public function get langDic():Object
		{
			return _langDic;
		}
		
		private function loadLangTxt():void
		{
			var rootFile:File = new File();
			var path:String = LangReplaceUi.exportUrl;//"C:\Users\Administrator\Desktop\ani-out\flashui"
			path += "\\" + LANG_FILE_CEHUA;
			var fl:File = rootFile.resolvePath(path);
			
			var request:URLRequest = new URLRequest(path);
			_urlLoader = new URLLoader();
			_urlLoader.addEventListener(Event.COMPLETE, onComplete1);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError1);
			_urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onLoadError2);
			_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError3);
			_urlLoader.data = null;
			_urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			_urlLoader.load(request);
		}
		private function onComplete1(e:Event):void
		{
			_urlLoader.removeEventListener(Event.COMPLETE, onComplete1);
			_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError1);
			_urlLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onLoadError2);
			_urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError3);
			
			var content:String = (_urlLoader.data);
			_langLoaded = true;
			
			var label:TextField = new TextField();
			label.text = content;
			trace("文本行数：" + label.numLines);
			var lines:int = label.numLines;
			var str:String;
			var arr:Array;
			for(var i:int=0; i<lines; i++){
				str = label.getLineText(i);
				str = StringTool.trim(str, ["\r","\n"]);
				if(str && str.indexOf(LANG_SPLIT) != -1){
					arr = str.split(LANG_SPLIT);
//					_langDic[arr[1]] = {"time":arr[0], "key":arr[1], "target":(arr[2] ? arr[2] : "")};
					if(replaceType == 1 || replaceType == 3){
						_langDic[arr[1]] = {"time":arr[0], "key":arr[1], "target":(arr[2] ? arr[2] : "")};
					}
					else if(replaceType == 2 || replaceType == 4){
						_langDic[arr[2]] = {"time":arr[0], "key":arr[2], "target":(arr[1] ? arr[1] : "")};
					}
					else{
						_langDic[arr[1]] = {"time":arr[0], "key":arr[1], "target":(arr[2] ? arr[2] : "")};
					}
				}
			}
			
			_urlLoader.close();
		}
		private function onLoadError1(e:Event):void
		{
			trace("加载是失败");
		}
		private function onLoadError2(e:HTTPStatusEvent):void
		{
			if(e.status >= 400){
				trace("加载是失败2,url="+e.target.url);
			}
		}
		private function onLoadError3(e:Event):void
		{
			trace("安全沙箱错误");
		}
		
		public function LangReplaceUiMan()
		{
			mTimer = new Timer(4);
			
			loadLangTxt();
		}
		public function pushPath(path:String):void
		{
			var index:int = path.lastIndexOf(".ui");
			if(index == path.length - 3){
				urlArr.push(path);
				
				if(!mTimer.running){
					mTimer.addEventListener(TimerEvent.TIMER, onTimerEvent);
					mTimer.start();
				}
			}
		}
		private function onTimerEvent(e:TimerEvent):void
		{
			if(!_langLoaded){
				LangReplaceUi.printMsg.text = "LangReplaceMan-正在读取已存在的语言txt。。。";
				return;
			}
			var time:int = new Date().time;
			if(time - curTime < 50){
				if(urlArr.length){
					var str:String = "LangReplaceMan-正在加载，还剩余：" + urlArr.length;
					var curUrl:String = urlArr.shift();
					LangReplaceUi.printMsg.text = str + "，\nurl=" + curUrl;
					try{
						new LangReplaceUi().doLoad(curUrl);
					}
					catch(e:*){
						trace("转换出错，路径为：" + curUrl);
					}
				}
				else{
					mTimer.removeEventListener(TimerEvent.TIMER, onTimerEvent);
					mTimer.stop();
					
					LangReplaceUi.printMsg.text = "LangReplaceMan-语言替换完成。";
				}
			}
			curTime = time;
		}
		
	}
}